const express = require('express');
const cors = require('cors');
const https = require('https');
const fs = require('fs');
const config = require('./config/default');
const routes = require('./routes');

const app = express();

const port = config.port; // 从配置文件中获取端口号

const httpsOptions = {
  key: fs.readFileSync(config.https.key),
  cert: fs.readFileSync(config.https.cert),
}
const corsOptions = config.corsOptions; // cors配置

app.use(cors(corsOptions));
app.use(express.json()); // 解析 JSON 请求体
app.use(express.urlencoded({ extended: true }));

const httpsServer = https.createServer(httpsOptions, app);

// 路由 /api代理
app.use('/api', routes);

app.get('/', (req, res) => {
  res.send('Welcome to the Supermarket Inventory Management System API');
});

// test
// app.post('/api/data',(req,res)=>{
//   console.log(req.body)
//   const data = req.body;
//   data.age += 1;
//   data.additionalInfo = "hello form backend";
//   data.currentTime = new Date().toLocaleString();
//   res.send(data)
// })

httpsServer.listen(port, () => {
  console.log(`Server is running on https://localhost:${port}`);
});
